package com.moneywelcome.orderassembly.apiImpl;

import com.moneywelcome.annnotation.RedisAnnotation;
import com.moneywelcome.orderassembly.service.DividendRecordService;
import com.moneywelcome.orderassembly.service.InvestRecordService;
import com.moneywelcome.orderassembly.service.RedeemRecordService;
import com.moneywelcome.orderassembly.service.UserFinancialService;
import com.moneywelcome.orderfacade.OrderCreatApi;
import com.moneywelcome.orderfacade.dto.DividendOrderDto;
import com.moneywelcome.orderfacade.dto.RedeemOrderDto;
import com.moneywelcome.orderfacade.dto.SubscribeOrderDto;
import com.moneywelcome.orderfacade.vo.DividendRecordVo;
import com.moneywelcome.orderfacade.vo.InvestRecordVo;
import com.moneywelcome.orderfacade.vo.RedeemRecordVo;
import com.moneywelcome.results.Result;
import com.moneywelcome.validate.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "记录生成")
public class OrderCreateApiImpl implements OrderCreatApi {
    @Autowired
    private InvestRecordService irs;
    @Autowired
    private RedeemRecordService rrs;
    @Autowired
    private DividendRecordService drs;
    @Autowired
    private UserFinancialService ufs;
    @ApiOperation("生成申购记录")
    /*
    生成申购与记录
     */
    @Override
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.OrderQueryApiImpl.subscribeRecord")
    public Result<SubscribeOrderDto> subscribeOrder(InvestRecordVo investRecordVo) {
        log.info("申购记录生成,{}", investRecordVo);
        return Result.success(irs.create(investRecordVo));
    }
    @ApiOperation("生成赎回记录")
    /*
    生成赎回记录
     */
    @Override
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.OrderQueryApiImpl.redeemRecord")
    public Result<RedeemOrderDto> redeemOrder(RedeemRecordVo redeemRecordVo) {
        log.info("赎回记录生成,{}", redeemRecordVo);
        return Result.success(rrs.create(redeemRecordVo));
    }
    @ApiOperation("完成申购记录")
    /**
     *
     * @param investSerial 申购交易编号
     * @param buyPrice 申购单价
     */
    @Override
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.OrderQueryApiImpl.subscribeRecord")
    public Result subscribeComplete() throws IOException {
        log.info("申购交易完成开始");
        irs.complete();
        return Result.success();
    }

    @ApiOperation("完成赎回记录")
    @Override
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.OrderQueryApiImpl.subscribeRecord")
    public Result redeemComplete() throws IOException {
        log.info("赎回交易开始完成");
        rrs.complete();
        return Result.success();
    }
    /*
    生成分红记录
     */
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.OrderQueryApiImpl.dividendRecord")
    @ApiOperation("生成分红申购记录")
    @Override
    public Result<DividendOrderDto> dividendOrder(DividendRecordVo dividendRecordVo) {
        log.info("分红纪录生成,{}", dividendRecordVo);
        return Result.success(drs.create(dividendRecordVo));
    }
    @Scheduled(cron = "0 0 18 * * ?")
//    @Scheduled(cron = "0 15 18 * * ? ")
    public void taskTrigger() throws IOException {
        log.info("定时任务开启");
        log.info("持仓收益更新");
        ufs.updateRevenue();
        log.info("申购记录完成");
        subscribeComplete();
        log.info("赎回记录完成");
        redeemComplete();
    }

}
